package com.link2room.aeriabots.service.impl;

import com.link2room.aeriabots.service.IWxKeyWordReplyService;
import com.link2room.common.jpa.result.QueryResult;
import com.link2room.common.service.ICommonService;
import com.link2room.aeriabots.dto.param.api.WxReplyKeyApiParamDto;
import com.link2room.aeriabots.entity.WxReplyTemplate;
import com.link2room.aeriabots.entity.WxReplyTemplateDetail;
import com.link2room.aeriabots.mapper.WxReplyKeyMapper;
import org.springframework.stereotype.Component;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 * Created by LN on 2016/12/2.
 */
@Component("wxKeyWordReplyService")

public class WxKeyWordReplyServiceImpl implements IWxKeyWordReplyService {
    @Resource
    private ICommonService commonService;

    @Override
    @Transactional
    public void save(WxReplyTemplate wxReplyTemplate, List<WxReplyTemplateDetail> articleEntities) throws Exception {
        commonService.save(wxReplyTemplate);
        commonService.saveList(articleEntities);
    }

    @Override
    @Transactional
    public void update(WxReplyTemplate wxReplyTemplate, List<WxReplyTemplateDetail> articleEntities , List<Long> delArticleList) throws Exception {
        commonService.updateById(wxReplyTemplate);
        commonService.saveOrMergeList(articleEntities);
        if(delArticleList != null ){
            for (Long id : delArticleList) {
                commonService.delete(WxReplyTemplateDetail.class,id);
            }
        }
    }

    @Override
    @Transactional
    public void delReply(WxReplyTemplate wxReplyTemplate, List<WxReplyTemplateDetail> articleEntities) throws Exception {
         WxReplyTemplate[]  keyWords = {wxReplyTemplate};
        Long[] wordIds = {wxReplyTemplate.getId()};
        commonService.delete(WxReplyTemplate.class,wordIds);
        if(articleEntities != null){
            List<Long> articleIds = articleEntities.stream().map(entity -> entity.getId()).collect(Collectors.toList());
            if(articleIds != null){
                commonService.delete(WxReplyTemplateDetail.class,articleIds.toArray());
            }
        }
    }

    @Override
    public QueryResult<WxReplyKeyMapper> findWxReplyKeyMapper(WxReplyKeyApiParamDto find, Integer currentPage, Integer pageSize) throws Exception {
        List queryParms = new ArrayList<>();
        List countParms = new ArrayList<>();

        String querySQL= findWxReplyKeyMapperQuerySql(find,queryParms);
        String countSQL = findWxReplyKeyMapperCountSql(find,countParms);
        QueryResult<WxReplyKeyMapper> rankingListDtoQueryResult=commonService.findByNative(querySQL,countSQL,queryParms,countParms, WxReplyKeyMapper.class,currentPage,pageSize);

        return rankingListDtoQueryResult;
    }

    private String findWxReplyKeyMapperQuerySql(WxReplyKeyApiParamDto find, List params){
        StringBuffer querySQL=new StringBuffer();
        querySQL.append("SELECT");
        querySQL.append(
                "  mapping.lhotel_group_code lhotelGroupCode," +
                "  mapping.lhotel_code lhotelCode," +
                "  mapping.id id," +
                "  mapping.key_id keyId," +
                "  mapping.template_id templateId," +
                "  template.template_name templateName," +
                "  mapping.key_type keyType," +
                "  mapping.key_value keyValue," +
                "  mapping.key_name keyName," +
                "  mapping.key_remark keyRemark," +
                "  mapping.is_halt isHalt," +
                "  mapping.list_order listOrder," +
                "  mapping.create_user createUser," +
                "  mapping.create_datetime createDateTime," +
                "  mapping.modify_user modifyUser," +
                "  mapping.modify_datetime modifyDateTime" +
                " From wx_reply_key_mapping mapping" +
                " LEFT JOIN wx_reply_template template ON mapping.template_id = template.template_id"
        );
        querySQL.append(" WHERE mapping.lhotel_group_code = ?1");
        params.add(find.getLhotelGroupCode());
        querySQL.append(" AND mapping.key_type = ?2");
        params.add(find.getKeyType());
        return querySQL.toString();
    }

    private String findWxReplyKeyMapperCountSql(WxReplyKeyApiParamDto find, List params){
        StringBuffer countSQL=new StringBuffer();
        countSQL.append(" SELECT count(1)");
        countSQL.append(" From wx_reply_key_mapping mapping");
        countSQL.append(" WHERE mapping.lhotel_group_code = ?1");
        params.add(find.getLhotelGroupCode());
        countSQL.append(" AND mapping.key_type = ?2");
        params.add(find.getKeyType());
        return countSQL.toString();

    }
}
